#pragma once
#include <string>
#include<vector>
#include"Skill.h"
#include "json.hpp"

using json = nlohmann::json;

class Equipment {
public:
    std::string name;
    std::string Info; 
    int attackBonus;
    int defenseBonus;
    int healthBonus;

    std::vector<Skill> skills; // 装备附带的技能

    void addSkill(const Skill& skill) {
        skills.push_back(skill);
    }

    // 获取装备的所有技能
    std::vector<Skill> getSkills() const {
        return skills;
    }

    Equipment(std::string n = "", std::string t = "",
        int atk = 0, int def = 0, int hp = 0,
        const std::vector<Skill>& sk = {})
        : name(n), Info(t), attackBonus(atk),
        defenseBonus(def), healthBonus(hp), skills(sk) {}

    json toJson() const {
        json skillsJson = json::array();
        for (const auto& skill : skills) {
            skillsJson.push_back(skill.toJson());
        }

        return {
            {"name", name},
            {"Info", Info},
            {"attackBonus", attackBonus},
            {"defenseBonus", defenseBonus},
            {"healthBonus", healthBonus},
            {"skills", skillsJson}
        };
    }

    // 从JSON反序列化
    static Equipment fromJson(const json& j) {
        std::vector<Skill> skills;
        if (j.contains("skills")) {
            for (const auto& skillJson : j["skills"]) {
                skills.push_back(Skill::fromJson(skillJson));
            }
        }

        return Equipment(
            j["name"].get<std::string>(),
            j["Info"].get<std::string>(),
            j["attackBonus"].get<int>(),
            j["defenseBonus"].get<int>(),
            j["healthBonus"].get<int>(),
            skills
        );
    }
};